import { sys } from 'cc';
import { _decorator, Component, Node } from 'cc';
import { wcadm } from './wxScripts/WeChatVideoAdManager';
import { Eventify } from 'cc';
import { System } from 'cc';
import { bdadm } from './ttScripts/ByteDanceVideoAdManager';
import { Timer } from '../libs/rxjs/cc3/CreatorSchedule';
import { Observable } from '../libs/rxjs/internal/Observable';
const { ccclass, property } = _decorator;

@ccclass('SDKVideoAdManager')
export class SDKVideoAdManager extends Eventify(System) {

    public static instance: SDKVideoAdManager;
    static ID = 'WeChat_system';

    registerSystem() {
        switch (sys.platform) {
            case sys.Platform.BYTEDANCE_MINI_GAME:
                bdadm.registerSystem();
                break;
            case sys.Platform.WECHAT_GAME:
                wcadm.registerSystem();
                break;
            default:
                break;
        }

    }

    init() {
        switch (sys.platform) {
            case sys.Platform.BYTEDANCE_MINI_GAME:
                bdadm.init();
                break;
            case sys.Platform.WECHAT_GAME:
                wcadm.init();
                break;
            default:
                break;
        }
    }

    showAds() {
        switch (sys.platform) {
            case sys.Platform.BYTEDANCE_MINI_GAME:
                return bdadm.showAds();
            case sys.Platform.WECHAT_GAME:
                return wcadm.showAds();
            default:
                globalThis.GameManager.instance.frontTipShow('获得奖励').subscribe();

                return new Observable(observer => {
                    Timer(.5).subscribe(_ => {
                        observer.next(true);
                        observer.complete();
                    });
                });
        }
    }

    public VideoAdInit() {
        if (sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
            bdadm.ByteDanceVideoAdInit();
        } else if (sys.platform == sys.Platform.WECHAT_GAME) {
            wcadm.WeChatVideoAdInit();
        }
    }

    public VideoAdShow() {
        if (sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
            bdadm.ByteDanceVideoAdShow();
        } else if (sys.platform == sys.Platform.WECHAT_GAME) {
            wcadm.WeChatVideoAdShow();
        }
    }

    public showInterstitial(callback: (result) => void, onShow: () => void) {
        if (sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
            bdadm.showInterstitial(callback, onShow);
        } else if (sys.platform == sys.Platform.WECHAT_GAME) {
            wcadm.showInterstitial(callback, onShow);
        }
    }

    public destroyInterstitial() {
        if (sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
            bdadm.destroyInterstitial();
        } else if (sys.platform == sys.Platform.WECHAT_GAME) {
            wcadm.destroyInterstitial();
        }
    }

    public interstitialOnLoadObserve(interstitiaAd) {
        if (sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
            bdadm.interstitialOnLoadObserve(interstitiaAd);
        } else if (sys.platform == sys.Platform.WECHAT_GAME) {
            wcadm.interstitialOnLoadObserve(interstitiaAd);
        }
    }

    public initBanner() {
        if (sys.platform == sys.Platform.WECHAT_GAME) {
            wcadm.initBanner();
        }
    }

    public showBanner() {
        if (sys.platform == sys.Platform.WECHAT_GAME) {
            wcadm.showBanner();
        }
    }

    public hideBanner() {
        if (sys.platform == sys.Platform.WECHAT_GAME) {
            wcadm.hideBanner();
        }
    }

    public destroyBanner() {
        if (sys.platform == sys.Platform.WECHAT_GAME) {
            wcadm.destroyBanner();
        }
    }
}

globalThis.SDKVideoAdManager = SDKVideoAdManager;

export const sdkadm = SDKVideoAdManager.instance = new SDKVideoAdManager();